package com.xwzn.resident;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
/**
 * @Description 启动界面
 * @Author HanKaiYang
 * @CreateDate 2019/12/23 11:05
 * @Version 1.0
 */
public class LauncherActivity extends AppCompatActivity {

    private Button btnLauncherTg;
    private static int time = 3;
    private static  boolean isShow = false;
    private  Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);
    }

    @Override
    protected void onStart() {
        super.onStart();
        //实例化intent
        intent = new Intent(LauncherActivity.this,MainActivity.class);
        //判断是否是第一次打开
        //如果是 则进入启动界面，否则 进入主界面
        if (isShow){
            startActivity(intent);
            finish();
        }else {
            initView();
        }
    }

    /**
     * @deprecated 初始化视图
     * @author HanKaiYang
     * @createDate 2019/12/23 11:12
     * @version 1.0
     */
    private void initView() {
        btnLauncherTg = (Button) findViewById(R.id.btn_launcher_tg);

        //时间计算
        final CountDownTimer timer = new CountDownTimer((time+2)*1000,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                if (time > 0){
                    btnLauncherTg.setText("0"+time+" 跳过");
                    time --;
                }else if (time == 0){
                    btnLauncherTg.setText("00 跳过");
                    this.cancel();
                    if (!isShow){
                        startActivity(intent);
                        finish();
                        isShow = true;
                    }
                }
            }
            @Override
            public void onFinish() {
                this.cancel();
            }
        };

        btnLauncherTg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.cancel();
                startActivity(intent);
                finish();
            }
        });
        //启动
        timer.start();
    }

}
